package com.neusoft.neuiotms.common.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.common.dto.SysTaskDTO;
import com.neusoft.neuiotms.common.service.SysTaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "定时任务")
@RestController
@RequestMapping(value = "/api/w/sysTask")
public class SysTaskController {

    @Autowired
    private SysTaskService sysTaskService;

    @ApiOperation("查询所有")
    @GetMapping
    public ResultListDTO<SysTaskDTO> index() {
        final List<SysTaskDTO> DTOs = this.sysTaskService.findAll();
        final ResultListDTO<SysTaskDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有")
    @GetMapping("/p")
    public PageResultDTO<SysTaskDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.sysTaskService.search(pageable, searchable));
    }

    @ApiOperation("创建定时任务")
    @PostMapping("")
    public ResultDTO<SysTaskDTO> create(@RequestBody final SysTaskDTO dto) {
        final SysTaskDTO result = this.sysTaskService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改定时任务")
    @PutMapping("/{id}")
    public ResultDTO<SysTaskDTO> update(@PathVariable final Long id,
            @RequestBody final SysTaskDTO dto) {
        final SysTaskDTO result = this.sysTaskService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("查看定时任务")
    @GetMapping("/{id}")
    public ResultDTO<SysTaskDTO> details(@PathVariable final Long id) {
        final SysTaskDTO dto = this.sysTaskService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除定时任务")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.sysTaskService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("手动刷新定时任务")
    @GetMapping("/refresh")
    public ResultDTO<Void> refresh() {
        this.sysTaskService.refreshJob();
        return ResultDTO.success();
    }
}
